package com.seek.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seek.commonutils.JwtUtils;
import com.seek.commonutils.ResponseResult;
import com.seek.commonutils.ordervo.CourseInfoOrder;
import com.seek.eduservice.client.OrderClient;
import com.seek.eduservice.entity.EduCourse;
import com.seek.eduservice.entity.subject.Chapter;
import com.seek.eduservice.entity.vo.front.CourseInfoVo;
import com.seek.eduservice.entity.vo.front.CourseVo;
import com.seek.eduservice.service.EduChapterService;
import com.seek.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: wml
 * @since: 2022/2/24 23:08
 */
@RestController
@RequestMapping("/eduservice/course")
public class CourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrderClient orderClient;

    // 条件查询带分页查询课程
    @GetMapping("pageCondition/{current}/{size}")
    public ResponseResult pageCondition(@PathVariable long current, @PathVariable long size, CourseVo courseVo) {
        Page<EduCourse> page = new Page<>(current, size);
        Map<String, Object> map = eduCourseService.getFrontCourseList(page, courseVo);
        return ResponseResult.ok().data(map);
    }

    // 根据课程 id 查询课程信息
    @GetMapping("getCourse/{courseId}")
    public ResponseResult getCourse(@PathVariable String courseId) {
        EduCourse eduCourse = eduCourseService.getById(courseId);
        System.out.println(eduCourse);
        return ResponseResult.ok().data("eduCourse", eduCourse);
    }

    // 根据课程 id 查询课程详情
    @GetMapping("getCourseInfo/{courseId}")
    public ResponseResult getCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        // 根据课程 id 查询课程信息
        CourseInfoVo courseInfoVo = eduCourseService.getBaseCourseInfo(courseId);
        // 根据课程 id 查询章节和小节
        List<Chapter> chapterAndVideoList = eduChapterService.getChapterAndVideoByCourseId(courseId);
        // 根据课程 id 和用户 id 查询订单表中的订单状态
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuyCourse = orderClient.isBuyCourse(courseId, memberId);
        return ResponseResult.ok().data("courseInfoVo", courseInfoVo)
                .data("chapterAndVideoList", chapterAndVideoList)
                .data("isBuy", isBuyCourse);
    }

    // 根据课程 id 增加该课程的浏览数量（+1）
    @PutMapping("updateCourseInfo/{id}")
    public ResponseResult updateCourseInfo(@PathVariable String id) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.select("id", "view_count");
        EduCourse eduCourse = eduCourseService.getOne(queryWrapper);
        eduCourse.setViewCount(eduCourse.getViewCount() + 1);
        eduCourseService.updateById(eduCourse);
        return ResponseResult.ok();
    }

    // 根据课程 id 查询课程信息
    @GetMapping("getCourseInfoOrder/{id}")
    public CourseInfoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseInfoVo courseInfoVo = eduCourseService.getBaseCourseInfo(id);
        CourseInfoOrder courseInfoOrder = new CourseInfoOrder();
        BeanUtils.copyProperties(courseInfoVo, courseInfoOrder);
        return courseInfoOrder;
    }

    // 根据课程 id 增加该课程的销售数量（+1）
    @PutMapping("updateCourseInfoOrder/{id}")
    public boolean updateCourseInfoOrder(@PathVariable String id) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.select("id", "buy_count");
        EduCourse eduCourse = eduCourseService.getOne(queryWrapper);
        eduCourse.setBuyCount(eduCourse.getBuyCount() + 1);
        eduCourseService.updateById(eduCourse);
        return true;
    }
}
